import { ref } from 'vue';
import {useDictStoreHook} from "@/store/modules/dict";

export type NumberDictDataType = CommonType.DictOption<number>;

export type StringDictDataType = CommonType.DictOption<string>;

export type BooleanDictDataType = CommonType.DictOption<boolean>;

/**
 * 根据字典类型获取字典 Option 列表
 *
 * @param dictType 字典类型
 * @returns 字典 Option 列表
 */
export const getDictOptions = (dictType: string) => {
  return useDictStoreHook().getDictionary(dictType) || ([] as CommonType.DictOption[]);
};

export const getIntDictOptions = (dictType: string): NumberDictDataType[] => {
  // 获得通用的 DictDataType 列表
  const dictOptions: CommonType.DictOption[] = getDictOptions(dictType);
  // 转换成 number 类型的 NumberDictDataType 类型
  // why 需要特殊转换：避免 IDEA 在 v-for="dict in getIntDictOptions(...)" 时，option 的 key 会告警
  const dictOption: NumberDictDataType[] = [];
  dictOptions.forEach((dict: CommonType.DictOption) => {
    dictOption.push({
      ...dict,
      value: Number.parseInt(`${dict.value}`, 10)
    });
  });
  return dictOption;
};

export const getStrDictOptions = (dictType: string): StringDictDataType[] => {
  // 获得通用的 DictDataType 列表
  const dictOptions: CommonType.DictOption[] = getDictOptions(dictType);
  // 转换成 string 类型的 StringDictDataType 类型
  // why 需要特殊转换：避免 IDEA 在 v-for="dict in getStrDictOptions(...)" 时，option 的 key 会告警
  const dictOption: StringDictDataType[] = [];
  dictOptions.forEach((dict: CommonType.DictOption) => {
    dictOption.push({
      ...dict,
      value: `${dict.value}`
    });
  });
  return dictOption;
};

export const getBoolDictOptions = (dictType: boolean): BooleanDictDataType[] => {
  const dictOptions: CommonType.DictOption[] = getDictOptions(dictType.toString());
  const dictOption: BooleanDictDataType[] = [];
  dictOptions.forEach((dict: CommonType.DictOption) => {
    dictOption.push({
      ...dict,
      value: `${dict.value}` === 'true'
    });
  });
  return dictOption;
};

/**
 * 获取指定字典类型的指定值对应的字典对象
 *
 * @param dictType 字典类型
 * @param value 字典值
 * @returns DictDataType 字典对象
 */
export const getDictObj = (dictType: string, value: any): CommonType.DictOption | undefined => {
  const dictOptions: CommonType.DictOption[] = getDictOptions(dictType);
  for (const dict of dictOptions) {
    if (dict.value === `${value}`) {
      return dict;
    }
  }
  return {
    label: value,
    value: value as string,
    colorType: 'default',
    cssClass: '',
    dictType: ''
  };
};

/**
 * 获得字典数据的文本展示
 *
 * @param dictType 字典类型
 * @param value 字典数据的值
 * @returns 字典名称
 */
export const getDictLabel = (dictType: string, value: any): string => {
  const dictOptions: CommonType.DictOption[] = getDictOptions(dictType);
  const dictLabel = ref('');
  dictOptions.forEach((dict: CommonType.DictOption) => {
    if (dict.value === `${value}`) {
      dictLabel.value = dict.label;
    }
  });
  return dictLabel.value;
};

export enum DICT_TYPE {
  COMMON_STATUS = 'common_status',
  COMMON_BOOLEAN_STRING = 'common_boolean_string',
  USER_TYPE = 'user_type',
  LOG_OPERATE_TYPE = 'log_operate_type',

  // ========== SYSTEM 模块 ==========
  SYSTEM_USER_SEX = 'system_user_sex',
  SYSTEM_MENU_TYPE = 'system_menu_type',
  SYSTEM_MENU_ICON_TYPE = 'system_menu_icon_type',
  SYSTEM_OAUTH2_GRANT_TYPE = 'system_oauth2_grant_type',
  SYSTEM_FILE_STORAGE = 'system_file_storage',
  SYSTEM_LOGIN_TYPE = 'system_login_type',
  SYSTEM_LOGIN_RESULT = 'system_login_result',
  SYSTEM_DATASOURCE_TYPE = 'system_database_type',
  SYSTEM_SMS_CHANNEL_CODE = 'system_sms_channel_code',
  SYSTEM_SMS_TEMPLATE_TYPE = 'system_sms_template_type',
  SYSTEM_SMS_SEND_STATUS = 'system_sms_send_status',
  SYSTEM_SMS_RECEIVE_STATUS = 'system_sms_receive_status',
  SYSTEM_NOTICE_TYPE = 'system_notice_type',
  SYSTEM_NOTICE_LEVEL = 'system_notice_level',

  // ========== Xxl执行器 模块 ==========
  EXECUTOR_REGISTER_TYPE = 'executor_register_type',
  EXECUTOR_STATUS = 'executor_status'
}
